package com.ruoyi.project.module.lys;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 消费详细Controller
 *
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/details1")
public class LysYqgConsumptionDetailsController extends BaseController
{
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;

    /**
     * 查询消费详细列表
     */
//    @RequiresPermissions("project:details:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails)
    {
        startPage();
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        return getDataTable(list);
    }

    /**
     * 导出消费详细列表
     */
//    @RequiresPermissions("project:details:export")
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails)
    {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
//    @RequiresPermissions("project:details:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
    }

    /**
     * 新增消费详细
     */
//    @RequiresPermissions("project:details:add")
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 修改消费详细
     */
//    @RequiresPermissions("project:details:edit")
    @Log(title = "消费详细", businessType = BusinessType.UPDATE)
    @PutMapping("/pp")
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        System.out.println("1000000");
        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 删除消费详细
     */
//    @RequiresPermissions("project:details:remove")
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }



    @Log(title = "退单", businessType = BusinessType.DELETE,logicalKey = "handBrand")
    @PostMapping(value = "/goods/{handid}/{note7}")
    public AjaxResult getInfo(@PathVariable("handid") String handid,@RequestBody Integer[] ids,@PathVariable("note7") int note7) {
        //通过路径接到handid，通过RequestBody接到ids
        System.out.println("1057557942"+handid+"handid"+note7);

        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();

//ppe
        for (int i:ids) {
            System.out.println(i);
            YqgConsumptionDetails ycd = new YqgConsumptionDetails();
            YqgConsumptionDetails p = yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(Long.valueOf(i));

     int parseint=Integer.parseInt(p.getQuantity());

           if(note7>parseint){
               throw new ServiceException("退单数量不能大于已点数量");

           }else if(note7<=0){
               throw new ServiceException("退单数量不能小于0");
           }
           else {
               Date d = new Date();
               String  l=String.valueOf(parseint-note7);
               p.setQuantity(l);
               p.setTotalAmount(String.valueOf(Integer.parseInt(l)*Integer.parseInt(p.getUnitPrice())));
               yqgConsumptionDetailsService.updateYqgConsumptionDetails(p);
               yqgConsumptionDetailsService.insertYqgConsumptionDetails(p);

               p.setQuantity(String.valueOf(note7));
               p.setTotalAmount(String.valueOf(note7*Integer.parseInt(p.getUnitPrice())));
               p.setChargebackTime(d);
               p.setConsumptionStatus("1");
               p.setChargebackProcessorName(sysUser.getNickName());
               p.setChargebackProcessorAccount(sysUser.getUserName());
               p.setReasonForChargeback(handid);
               yqgConsumptionDetailsService.updateYqgConsumptionDetails(p);


           }
        }
        return success();
    }





}
